package com.tarena.lbs.service.impl;

import com.tarena.lbs.entity.Coupon;
import com.tarena.lbs.mapper.CouponMapper;
import com.tarena.lbs.mapper.UserCouponsMapper;
import com.tarena.lbs.service.CouponService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    private static final Logger log = LoggerFactory.getLogger(CouponServiceImpl.class);

    @Autowired
    private CouponMapper couponMapper;

    // 新增 userCouponsMapper 成员变量
    @Autowired
    private UserCouponsMapper userCouponsMapper;

    @Override
    public boolean addCoupon(Coupon coupon) {
        int result = couponMapper.insertCoupon(coupon);
        return result > 0;
    }

    @Override
    public Coupon getCouponById(Integer id) {
        return couponMapper.getCouponById(id);
    }

    @Override
    public List<Coupon> listCouponsByActivityId(Integer activityId) {
        return couponMapper.listCouponsByActivityId(activityId);
    }

    @Override
    @Transactional
    public boolean updateCouponStock(Integer couponId, Integer num) {
        int result = couponMapper.updateCouponStock(couponId, num);
        return result > 0;
    }

    @Override
    @Transactional
    public boolean deleteCoupon(Integer id) {
        // 参数校验
        if (id == null) {
            log.error("删除优惠券失败：ID为空");
            return false;
        }

        // 检查优惠券是否存在
        Coupon coupon = couponMapper.getCouponById(id);
        if (coupon == null) {
            log.error("删除优惠券失败：优惠券不存在，id={}", id);
            return false;
        }

        // 执行删除操作
        int result = couponMapper.deleteCoupon(id);
        if (result > 0) {
            log.info("删除优惠券成功，id={}", id);
            return true;
        } else {
            log.error("删除优惠券失败，id={}", id);
            return false;
        }
    }

    @Override
    @Transactional
    public boolean batchDeleteCoupons(List<Integer> ids) {
        // 参数校验
        if (ids == null || ids.isEmpty()) {
            log.error("批量删除优惠券失败：ID列表为空");
            return false;
        }

        // 执行批量删除
        int result = couponMapper.batchDeleteCoupons(ids);
        if (result > 0) {
            log.info("批量删除优惠券成功，ids={}, 删除数量={}", ids, result);
            return true;
        } else {
            log.error("批量删除优惠券失败，ids={}", ids);
            return false;
        }
    }

    @Override
    @Transactional
    public List<Coupon> listCouponsByActivityIdWithoutActivityIdField(Integer activityId) {
        // 假设在 UserCouponsMapper 中添加了 getCouponIdsByActivityId 方法
        List<Integer> couponIds = userCouponsMapper.getCouponIdsByActivityId(activityId);
        if (couponIds.isEmpty()) {
            return null;
        }
        return couponMapper.listCouponsByIds(couponIds);
    }
}